<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单例</title>
</head>
<body>
<script>
    //ES5
    var singletonManager = (function () {
        var _manager;
        function Manager(name) {
            this.name = name;
        }
        Manager.prototype.getName = function () {
            console.log(this.name);
        };
        return function (name) {
            if (!_manager) {
                _manager = new Manager(name);
            }
            return _manager;
        }
    })();

    singletonManager('a').getName();
    singletonManager('b').getName();

    //ES6
    class Manager {
        constructor(name) {
            if (!Manager.instance) {
                this.name = name;
                Manager.instance = this;
            }
            return Manager.instance;
        }
    }
    var m1 = new Manager('a');
    var m2 = new Manager('b');
    console.log(m1 === m2);
</script>
</body>
</html>